package com.wfuhui.modules.jieyue.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wfuhui.common.utils.NumberUtil;
import com.wfuhui.modules.book.dao.BookDao;
import com.wfuhui.modules.jieyue.dao.JieyueDao;
import com.wfuhui.modules.jieyue.entity.JieyueEntity;
import com.wfuhui.modules.jieyue.service.JieyueService;


@Service("JieyueService")
public class JieyueServiceImpl implements JieyueService {
	@Autowired
	private JieyueDao JieyueDao;
	@Autowired
	private BookDao bookDao;
	
	@Override
	public JieyueEntity queryObject(Integer id){
		JieyueEntity Jieyue = JieyueDao.queryObject(id);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", Jieyue.getId());
		List<JieyueEntity> JieyueBookList = JieyueDao.queryList(params);
		Jieyue.setJieyueEntities(JieyueBookList);
		return Jieyue;
	}
	
	@Override
	public List<JieyueEntity> queryList(Map<String, Object> map){
		List<JieyueEntity> JieyueList = JieyueDao.queryList(map);
		for (JieyueEntity JieyueEntity : JieyueList) {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("JieyueId", JieyueEntity.getId());
			List<JieyueEntity> JieyueBookList = JieyueDao.queryList(params);
			JieyueEntity.setJieyueEntities(JieyueBookList);
		}
		return JieyueList;
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return JieyueDao.queryTotal(map);
	}
	
	@Override
	public void save(JieyueEntity jieyue){

		JieyueDao.save(jieyue);
	}
	
	@Override
	public void update(JieyueEntity Jieyue){
		JieyueDao.update(Jieyue);
	}
	
	@Override
	public void delete(Integer JieyueId){
		JieyueDao.delete(JieyueId);
	}
	
	@Override
	public void deleteBatch(Integer[] JieyueIds){
		JieyueDao.deleteBatch(JieyueIds);
	}

	@Override
	public void createOrder(JieyueEntity orderEntity) {

	}

	@Override
	public void updateByOrderNumber(JieyueEntity order) {

	}

	@Override
	public List<Map<String, String>> queryOrderCount() {
		return null;
	}

	@Override
	@Transactional
	public void createJieyue(JieyueEntity Jieyue) {
		Jieyue.setOrderNum(NumberUtil.getOrderNumber());
		JieyueDao.save(Jieyue);

	}

	@Override
	public void updateByJieyueNumber(JieyueEntity Jieyue) {
		JieyueDao.updateByOrderNumber(Jieyue);
	}

	@Override
	public List<Map<String, String>> queryJieyueCount() {
		return JieyueDao.queryOrderCount();
	}

	@Override
	@Transactional
	public void returnBook(JieyueEntity jieyue) {
		jieyue.setOrderStatus(2);
		jieyue.setEndDate(new Date());
		JieyueDao.update(jieyue);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("JieyueId", jieyue.getId());
		List<JieyueEntity> JieyueBookList = JieyueDao.queryList(map);
		for (JieyueEntity JieyueBook : JieyueBookList) {
			bookDao.addStock(JieyueBook.getBookid(), 1);
		}
	}
	
}
